var typeM = {
		's' : 'string',
		'n' : 'int',
		'd' : 'date'
	};
function createDataRecord(data){
	return {
			name : data.name,
			type : typeM[data.datatype]
		};
}
function createColm(data){
	
	return  {
			id : data.name,
			header : data.cnname,
			tooltip : '列名:' +  data.name,
			dataIndex :  data.name,
			width : 100,
			sortable : false,
			checksortable : true
		};
}
function dataRecordAndColModelArray(cols){
	var dataRecordArray = new Array(cols.length);
	var colm = new Array(cols.length);
	var name;
	var cnName;
	
	for (var i = 0; i < cols.length; i++) {
		var data = cols[i].data;
		dataRecordArray[i]=createDataRecord(data);
		colm[i] =createColm(data);
	}
	return {dataRecord :dataRecordArray,colModel :colm}
}


function dataRecordAndColModel(recordColModel) {
	var dataRecord = Ext.data.Record.create([].concat(recordColModel.dataRecord));
	var colModel = new Ext.grid.ColumnModel({
				listeners : {
					columnmoved : function() {
					}
				},
				columns : [].concat(recordColModel.colModel)
			});
	return {
		dataRecord : dataRecord,
		colModel : colModel
	};
}

function getColArray(grid){
		var ss=[];
		var length=grid.colModel.config.length;
		for(var i=0;i<length;i++){
			var columns=grid.colModel.config[i];
			
			ss.push({
				id:columns.id,
				header:columns.header,
				dataIndex:columns.dataIndex,
				checksortable:columns.checksortable===true,
				hidden:columns.hidden,
				fixed:columns.fixed,
				resizabl:columns.resizabl,
				locked:columns.locked,
				align:columns.align,
				width:columns.width
			});
		}
	return ss;
}


function dataRecordAndColModelArray2(cols,oldcols){
	var dataRecord=oldcols.dataRecord;
	var colModel=oldcols.colModel;
	var newdataRecord=[];
	var newcolModel=[];
	
	var name=null;
	for(var i=0;i<cols.length;i++){
		var data=cols[i].data;
		name=data.name;
		if(!isContains(dataRecord,name)){
			newdataRecord.push(createDataRecord(data));
			newcolModel.push(createColm(data));
		}
	}
	deleteContains(cols,dataRecord,'name');
	deleteContains(cols,colModel,'id');

			if(newdataRecord.length>0){
				dataRecord=dataRecord.concat(newdataRecord);
				colModel=colModel.concat(newcolModel);
			}
	return {
		dataRecord : dataRecord,
		colModel : colModel
	};
}

function deleteContains(cols,array,colname){
	for(var i=array.length-1;i>=0;i--){
		if(!isContainsData(cols,array[i][colname])){
			array.splice(i,1);
		}
	}
}
function isContainsData(array,value){
 	var name=null;
 	for (var i = 0; i < array.length; i++) {
 		if(array[i].data.name===value)
 			return true;
 	}
	return false;
}
function isContains(array,value){
 	for (var i = 0; i < array.length; i++) {
 		if(array[i].name===value)
 			return true;
 	}
	
	return false;

}


function openGridCfgWindow(cols,oldcols,okfunc) {
	var recordColModel=oldcols?dataRecordAndColModelArray2(cols,oldcols):dataRecordAndColModelArray(cols);
	var obj = dataRecordAndColModel(recordColModel);
	var dataRecord = obj.dataRecord;
	var colModel = obj.colModel;
	var store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : ''
						}),
				reader : new Ext.data.JsonReader({
							totalProperty : 'results'
						}, dataRecord)
			});
	var grid = new Ext.grid.GridPanel({
		store : store,
		recordArray:recordColModel.dataRecord,
		cm : colModel,
		pand : true,
		listeners : {
			'headerdblclick' : function(grid, Number, e) {

				if (this.pand) {
					this.pand = false;
					if (grid.tf != null) {
						grid.tf.destroy();
						grid.tf = null;
					}
					grid.tf = new Ext.form.TextField({
						grow : true,
						width : 15,
						height : 16,
						listeners : {
							blur : function() {
								if (this.getValue() != '')
									grid.colModel.setColumnHeader(this.colnumber, this.getValue());
								else
									grid.colModel.setColumnHeader(this.colnumber,grid.colModel.getColumnHeader(this.colnumber));
								this.pand = true;
							}
						}
					});

					e.target.innerText = '';
					grid.tf.colnumber = Number;
					grid.tf.render(e.target);
					grid.tf.setValue(grid.colModel.getColumnHeader(Number));
					grid.tf.focus();

				} else {

					grid.tf.blur();
					this.pand = true;
				}
			},
			render : function() {
				this.sortableItem = new Ext.menu.CheckItem({
					text : "是否可排序",
					checked : false,
					checkHandler : function(item, checked) {
						grid.colModel.config[grid.view.hdCtxIndex].checksortable = checked;
					},
					hideOnClick : false
				});

				this.dataIndexItem = new Ext.menu.Item({
							text : "",
							hideOnClick : false
						});

				this.infotext = new Ext.form.TextField({});	
			
				this.view.hmenu.on('beforeshow', function() {
							var config = grid.colModel.config[grid.view.hdCtxIndex];
							var checked = config.checksortable;
							grid.dataIndexItem.setText("列名:" + config.dataIndex);
							grid.sortableItem.setChecked(checked);
						});
				grid.view.hmenu.add('-', this.sortableItem, this.dataIndexItem,'-');
			}
		},
		width : 900,
		height : 60
	});
	var window = new Ext.Window({
				title : "表格列设置",
				width : 920,
				closeAction : 'close',
				plain : true,
				labelAlign : "right",
				buttonAlign : "center",
				resizable : false,
				modal : true,
				//height : 260,
				padding : "2px 2px",
				items : [grid],
				buttons : [{
					text : "确定",
					handler : function() {
						okfunc({
							dataRecord : grid.recordArray,
							colModel : getColArray(grid)
						});
					window.close();
					}
				}, {
					text : "关闭",
					handler : function() {
						window.close();
					}
				}]
			});

	window.show();
}